/*
 ============================================================================
 Name		: CamEngine.cpp
 Author	  : Lenka Litvova
 Version	 : 1.0
 Copyright   : Copyright belongs to Lenka Litvova
 Description : CCamEngine implementation
 ============================================================================
 */

#include <caeengine.h>
#include <eikspmod.h>
#include <eikenv.h>

#include "FRamI_UIAppUi.h"

#include "CamEngine.h"

// -----------------------------------------------------------------------------
// CCamEngine::CCamEngine()
// Default constructor
// -----------------------------------------------------------------------------
//
CCamEngine::CCamEngine(MCameraReceiver  *aView, CFRamI_UIAppUi *aAppUi):iContainer(aView), iAppUi(aAppUi)
	{
	}
	
// -----------------------------------------------------------------------------
// CCamEngine::~CCamEngine()
// Destructor
// -----------------------------------------------------------------------------
//
CCamEngine::~CCamEngine()
	{	
	if(iCaeEngine)
		{
		iCaeEngine->StopViewFinder();
		delete iCaeEngine;
		iCaeEngine=NULL;
		}
	}
	
// -----------------------------------------------------------------------------
// CCamEngine::NewLC()
// Symbian New method for Construction.
// -----------------------------------------------------------------------------
//
CCamEngine* CCamEngine::NewLC(MCameraReceiver *aView, CFRamI_UIAppUi *aAppUi)
	{
	CCamEngine* self = new (ELeave)CCamEngine(aView, aAppUi);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}
	
// -----------------------------------------------------------------------------
// CCamEngine::NewL()
// Symbian New method for Construction.
// -----------------------------------------------------------------------------
//
CCamEngine* CCamEngine::NewL(MCameraReceiver *aView, CFRamI_UIAppUi *aAppUi)
	{
	CCamEngine* self=CCamEngine::NewLC(aView, aAppUi);
	CleanupStack::Pop(); // self;
	return self;
	}

// -----------------------------------------------------------------------------
// CCamEngine::ConstructL()
// -----------------------------------------------------------------------------
//
void CCamEngine::ConstructL()
	{
	iCaeEngine=CCaeEngine::NewL(1);

	iCaeEngine->SetCamAppEngineObserver(*this);
	
   	//InitCameraL();
   	}

// -----------------------------------------------------------------------------
// CCamEngine::InitCameraL()
// Initialise the camera for still image capture
// -----------------------------------------------------------------------------
//
void CCamEngine::InitCameraL()
	{
		iViewSize = TSize(240,245);
		iViewfinderMode = ETrue;
		iCaeEngine->InitL();
		iCapture = ETrue;
	}

// -----------------------------------------------------------------------------
// CCamEngine::InitCameraL()
// Initialise the camera for still image capture
// -----------------------------------------------------------------------------
//
void CCamEngine::InitCameraL(TSize aViewSize)
	{
		iViewSize = aViewSize;
		iViewfinderMode = ETrue;
		iCaeEngine->InitL();
		iCapture = EFalse;
	}

// -----------------------------------------------------------------------------
// CCamEngine::CaptureSingleShot()
// Capture single still image
// -----------------------------------------------------------------------------
//
void CCamEngine::CaptureImage()
	{
/*	iCaeEngine->SetSnapImageCreation(ETrue);
	iCaeEngine->CaptureStill();
if(!iCaeEngine->IsViewFinding())
		{
			TSize size = TSize(240,245);
			TRAPD(err, iCaeEngine->StartViewFinderBitmapsL(size));
			if(err)
				RDebug::Print(_L("tu"));
		}*/
	iCapture = ETrue;
	}

// -----------------------------------------------------------------------------
// CCamEngine::StartViewFinderL()
// starts the view finder
// -----------------------------------------------------------------------------
//
void CCamEngine::StartViewFinderL()
	{
	iCaeEngine->StartViewFinderBitmapsL(iViewSize);
	}

void CCamEngine::StopViewFinderL()
	{
	if(iCaeEngine->IsViewFinding())
		iCaeEngine->StopViewFinder();
	}

// -----------------------------------------------------------------------------
// CCamEngine::McaeoInitComplete()
// From MCamAppEngineObserver
// -----------------------------------------------------------------------------
//
void  CCamEngine::McaeoInitComplete (TInt aError)
	{
		if(!aError)
		{
			TInt err;
			TRAP(err,iCaeEngine->PrepareStillCaptureL(1));	
			if(iViewfinderMode)
				TRAP(err, StartViewFinderL());
		}

	}

// -----------------------------------------------------------------------------
// CCamEngine::McaeoStillPrepareComplete()
// From MCamAppEngineObserver
// -----------------------------------------------------------------------------
//	
void CCamEngine::McaeoStillPrepareComplete (TInt /*aError*/)
	{
	
	}
// -----------------------------------------------------------------------------
// CCamEngine::McaeoVideoPrepareComplete()
// From MCamAppEngineObserver
// -----------------------------------------------------------------------------
//
void CCamEngine::McaeoVideoPrepareComplete (TInt /*aError*/)
	{
	
	}

// -----------------------------------------------------------------------------
// CCamEngine::McaeoViewFinderFrameReady()
// From MCamAppEngineObserver
// -----------------------------------------------------------------------------
//
void CCamEngine::McaeoViewFinderFrameReady (CFbsBitmap &aFrame, TInt aError)
	{
	if(iCapture)
	{
		iCaeEngine->StopViewFinder();
		iAppUi->PhotoReady(aFrame);
	}
	else if(!aError)
		{
			if(iContainer != NULL)
			iContainer->DrawImage(aFrame);
		}
	
		
	}
// -----------------------------------------------------------------------------
// CCamEngine::McaeoSnapImageReady()
// From MCamAppEngineObserver
// -----------------------------------------------------------------------------
//
void CCamEngine::McaeoSnapImageReady (const CFbsBitmap &aBitmap, TInt /*aError*/)
	{
	//SaveImage(aBitmap);
		//TSize s = aBitmap.SizeInPixels();
		//iAppUi->PhotoReady(aBitmap);
	}
// -----------------------------------------------------------------------------
// CCamEngine::McaeoStillImageReady()
// From MCamAppEngineObserver
// -----------------------------------------------------------------------------
//
void CCamEngine::McaeoStillImageReady (CFbsBitmap *aBitmap, HBufC8 *aData, TInt aError)
	{
		//RDebug::Print(_L("ready"));
		//iAppUi->PhotoReady(&aBitmap);
	}
// -----------------------------------------------------------------------------
// CCamEngine::McaeoVideoRecordingOn()
// From MCamAppEngineObserver
// -----------------------------------------------------------------------------
//
void CCamEngine::McaeoVideoRecordingOn (TInt /*aError*/)
	{
	
	}
// -----------------------------------------------------------------------------
// CCamEngine::McaeoVideoRecordingPaused()
// From MCamAppEngineObserver
// -----------------------------------------------------------------------------
//
void CCamEngine::McaeoVideoRecordingPaused (TInt /*aError*/)
	{
	
	}
// -----------------------------------------------------------------------------
// CCamEngine::McaeoVideoRecordingComplete()
// From MCamAppEngineObserver
// -----------------------------------------------------------------------------
//
void CCamEngine::McaeoVideoRecordingComplete (TInt /*aError*/)
	{

	}
// -----------------------------------------------------------------------------
// CCamEngine::McaeoVideoRecordingTimes()
// From MCamAppEngineObserver
// -----------------------------------------------------------------------------
//
void CCamEngine::McaeoVideoRecordingTimes (TTimeIntervalMicroSeconds /*aTimeElapsed*/, TTimeIntervalMicroSeconds /*aTimeRemaining*/, TInt /*aError*/)
	{
		
	}

